package com.jt.util;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

    // 1.获取cookie对象
    public static Cookie getCookie(HttpServletRequest request, String cookieName){
        Cookie[] cookies = request.getCookies();
        if ( cookies != null && cookies.length > 0 ){
            for (Cookie cookie : cookies){
                if (cookieName.equals(cookie.getName())){
                    return cookie;
                }
            }
        }
        return null;
    }


    // 2.获取cookie值
    public static String getCookieValue(HttpServletRequest request, String cookieName){
        Cookie cookie = getCookie(request, cookieName);
        return cookie == null?null:cookie.getValue();
    }


    // 3.新增Cookie/删除cookie
    public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue, String path, String domain, Integer seconds){
        Cookie cookie = new Cookie(cookieName,cookieValue);
        cookie.setPath(path);
        cookie.setDomain(domain);
        cookie.setMaxAge(seconds);
        response.addCookie(cookie);
    }



}
